Use Context in Article::delete for messages
[lhc/web/wiklou.git] / tests / qunit / data / testrunner.js
1 /*global CompletenessTest, sinon */
2 /*jshint evil: true */
3 ( function ( $, mw, QUnit ) {
4 'use strict';
5
6 var mwTestIgnore, mwTester,
7 addons,
8 ELEMENT_NODE = 1,
9 TEXT_NODE = 3;
10
11 /**
12 * Add bogus to url to prevent IE crazy caching
13 *
14 * @param value {String} a relative path (eg. 'data/foo.js'
15 * or 'data/test.php?foo=bar').
16 * @return {String} Such as 'data/foo.js?131031765087663960'
17 */
18 QUnit.fixurl = function ( value ) {
19 return value + (/\?/.test( value ) ? '&' : '?')
20 + String( new Date().getTime() )
21 + String( parseInt( Math.random() * 100000, 10 ) );
22 };
23
24 /**
25 * Configuration
26 */
27
28 // When a test() indicates asynchronicity with stop(),
29 // allow 30 seconds to pass before killing the test(),
30 // and assuming failure.
31 QUnit.config.testTimeout = 30 * 1000;
32
33 // Add a checkbox to QUnit header to toggle MediaWiki ResourceLoader debug mode.
34 QUnit.config.urlConfig.push( {
35 id: 'debug',
36 label: 'Enable ResourceLoaderDebug',
37 tooltip: 'Enable debug mode in ResourceLoader'
38 } );
39
40 QUnit.config.requireExpects = true;
41
42 /**
43 * Load TestSwarm agent
44 */
45 // Only if the current url indicates that there is a TestSwarm instance watching us
46 // (TestSwarm appends swarmURL to the test suites url it loads in iframes).
47 // Otherwise this is just a simple view of Special:JavaScriptTest/qunit directly,
48 // no point in loading inject.js in that case. Also, make sure that this instance
49 // of MediaWiki has actually been configured with the required url to that inject.js
50 // script. By default it is false.
51 if ( QUnit.urlParams.swarmURL && mw.config.get( 'QUnitTestSwarmInjectJSPath' ) ) {
52 jQuery.getScript( QUnit.fixurl( mw.config.get( 'QUnitTestSwarmInjectJSPath' ) ) );
53 }
54
55 /**
56 * CompletenessTest
57 *
58 * Adds toggle checkbox to header
59 */
60 QUnit.config.urlConfig.push( {
61 id: 'completenesstest',
62 label: 'Run CompletenessTest',
63 tooltip: 'Run the completeness test'
64 } );
65
66 /**
67 * SinonJS
68 *
69 * Glue code for nicer integration with QUnit setup/teardown
70 * Inspired by http://sinonjs.org/releases/sinon-qunit-1.0.0.js
71 * Fixes:
72 * - Work properly with asynchronous QUnit by using module setup/teardown
73 * instead of synchronously wrapping QUnit.test.
74 */
75 sinon.assert.fail = function ( msg ) {
76 QUnit.assert.ok( false, msg );
77 };
78 sinon.assert.pass = function ( msg ) {
79 QUnit.assert.ok( true, msg );
80 };
81 sinon.config = {
82 injectIntoThis: true,
83 injectInto: null,
84 properties: ['spy', 'stub', 'mock', 'sandbox'],
85 // Don't fake timers by default
86 useFakeTimers: false,
87 useFakeServer: false
88 };
89 ( function () {
90 var orgModule = QUnit.module;
91
92 QUnit.module = function ( name, localEnv ) {
93 localEnv = localEnv || {};
94 orgModule( name, {
95 setup: function () {
96 var config = sinon.getConfig( sinon.config );
97 config.injectInto = this;
98 sinon.sandbox.create( config );
99
100 if ( localEnv.setup ) {
101 localEnv.setup.call( this );
102 }
103 },
104 teardown: function () {
105 this.sandbox.verifyAndRestore();
106
107 if ( localEnv.teardown ) {
108 localEnv.teardown.call( this );
109 }
110 }
111 } );
112 };
113 }() );
114
115 // Initiate when enabled
116 if ( QUnit.urlParams.completenesstest ) {
117
118 // Return true to ignore
119 mwTestIgnore = function ( val, tester ) {
120
121 // Don't record methods of the properties of constructors,
122 // to avoid getting into a loop (prototype.constructor.prototype..).
123 // Since we're therefor skipping any injection for
124 // "new mw.Foo()", manually set it to true here.
125 if ( val instanceof mw.Map ) {
126 tester.methodCallTracker.Map = true;
127 return true;
128 }
129 if ( val instanceof mw.Title ) {
130 tester.methodCallTracker.Title = true;
131 return true;
132 }
133
134 // Don't record methods of the properties of a jQuery object
135 if ( val instanceof $ ) {
136 return true;
137 }
138
139 // Don't iterate over the module registry (the 'script' references would
140 // be listed as untested methods otherwise)
141 if ( val === mw.loader.moduleRegistry ) {
142 return true;
143 }
144
145 return false;
146 };
147
148 mwTester = new CompletenessTest( mw, mwTestIgnore );
149 }
150
151 /**
152 * Test environment recommended for all QUnit test modules
153 *
154 * Whether to log environment changes to the console
155 */
156 QUnit.config.urlConfig.push( 'mwlogenv' );
157
158 /**
159 * Reset mw.config and others to a fresh copy of the live config for each test(),
160 * and restore it back to the live one afterwards.
161 * @param localEnv {Object} [optional]
162 * @example (see test suite at the bottom of this file)
163 * </code>
164 */
165 QUnit.newMwEnvironment = ( function () {
166 var warn, log, liveConfig, liveMessages;
167
168 liveConfig = mw.config.values;
169 liveMessages = mw.messages.values;
170
171 function suppressWarnings() {
172 warn = mw.log.warn;
173 mw.log.warn = $.noop;
174 }
175
176 function restoreWarnings() {
177 if ( warn !== undefined ) {
178 mw.log.warn = warn;
179 warn = undefined;
180 }
181 }
182
183 function freshConfigCopy( custom ) {
184 var copy;
185 // Tests should mock all factors that directly influence the tested code.
186 // For backwards compatibility though we set mw.config to a fresh copy of the live
187 // config. This way any modifications made to mw.config during the test will not
188 // affect other tests, nor the global scope outside the test runner.
189 // This is a shallow copy, since overriding an array or object value via "custom"
190 // should replace it. Setting a config property means you override it, not extend it.
191 // NOTE: It is important that we suppress warnings because extend() will also access
192 // deprecated properties and trigger deprecation warnings from mw.log#deprecate.
193 suppressWarnings();
194 copy = $.extend( {}, liveConfig, custom );
195 restoreWarnings();
196
197 return copy;
198 }
199
200 function freshMessagesCopy( custom ) {
201 return $.extend( /*deep=*/true, {}, liveMessages, custom );
202 }
203
204 log = QUnit.urlParams.mwlogenv ? mw.log : function () {};
205
206 return function ( localEnv ) {
207 localEnv = $.extend( {
208 // QUnit
209 setup: $.noop,
210 teardown: $.noop,
211 // MediaWiki
212 config: {},
213 messages: {}
214 }, localEnv );
215
216 return {
217 setup: function () {
218 log( 'MwEnvironment> SETUP for "' + QUnit.config.current.module
219 + ': ' + QUnit.config.current.testName + '"' );
220
221 // Greetings, mock environment!
222 mw.config.values = freshConfigCopy( localEnv.config );
223 mw.messages.values = freshMessagesCopy( localEnv.messages );
224 this.suppressWarnings = suppressWarnings;
225 this.restoreWarnings = restoreWarnings;
226
227 localEnv.setup.call( this );
228 },
229
230 teardown: function () {
231 var timers;
232 log( 'MwEnvironment> TEARDOWN for "' + QUnit.config.current.module
233 + ': ' + QUnit.config.current.testName + '"' );
234
235 localEnv.teardown.call( this );
236
237 // Farewell, mock environment!
238 mw.config.values = liveConfig;
239 mw.messages.values = liveMessages;
240
241 // As a convenience feature, automatically restore warnings if they're
242 // still suppressed by the end of the test.
243 restoreWarnings();
244
245 // Check for incomplete animations/requests/etc and throw
246 // error if there are any.
247 if ( $.timers && $.timers.length !== 0 ) {
248 timers = $.timers.length;
249 // Tests shoulld use fake timers or wait for animations to complete
250 $.each( $.timers, function ( i, timer ) {
251 var node = timer.elem;
252 mw.log.warn( 'Unfinished animation #' + i + ' in ' + timer.queue + ' queue on ' +
253 mw.html.element( node.nodeName.toLowerCase(), $(node).getAttrs() )
254 );
255 } );
256 // Force animations to stop to give the next test a clean start
257 $.fx.stop();
258
259 throw new Error( 'Unfinished animations: ' + timers );
260 }
261 if ( $.active !== undefined && $.active !== 0 ) {
262 // Test may need to use fake XHR, wait for requests or
263 // call abort().
264 throw new Error( 'Unfinished AJAX requests: ' + $.active );
265 }
266 }
267 };
268 };
269 }() );
270
271 // $.when stops as soon as one fails, which makes sense in most
272 // practical scenarios, but not in a unit test where we really do
273 // need to wait until all of them are finished.
274 QUnit.whenPromisesComplete = function () {
275 var altPromises = [];
276
277 $.each( arguments, function ( i, arg ) {
278 var alt = $.Deferred();
279 altPromises.push( alt );
280
281 // Whether this one fails or not, forwards it to
282 // the 'done' (resolve) callback of the alternative promise.
283 arg.always( alt.resolve );
284 } );
285
286 return $.when.apply( $, altPromises );
287 };
288
289 /**
290 * Recursively convert a node to a plain object representing its structure.
291 * Only considers attributes and contents (elements and text nodes).
292 * Attribute values are compared strictly and not normalised.
293 *
294 * @param {Node} node
295 * @return {Object|string} Plain JavaScript value representing the node.
296 */
297 function getDomStructure( node ) {
298 var $node, children, processedChildren, i, len, el;
299 $node = $( node );
300 if ( node.nodeType === ELEMENT_NODE ) {
301 children = $node.contents();
302 processedChildren = [];
303 for ( i = 0, len = children.length; i < len; i++ ) {
304 el = children[i];
305 if ( el.nodeType === ELEMENT_NODE || el.nodeType === TEXT_NODE ) {
306 processedChildren.push( getDomStructure( el ) );
307 }
308 }
309
310 return {
311 tagName: node.tagName,
312 attributes: $node.getAttrs(),
313 contents: processedChildren
314 };
315 } else {
316 // Should be text node
317 return $node.text();
318 }
319 }
320
321 /**
322 * Gets structure of node for this HTML.
323 *
324 * @param {string} html HTML markup for one or more nodes.
325 */
326 function getHtmlStructure( html ) {
327 var el = $( '<div>' ).append( html )[0];
328 return getDomStructure( el );
329 }
330
331 /**
332 * Add-on assertion helpers
333 */
334 // Define the add-ons
335 addons = {
336
337 // Expect boolean true
338 assertTrue: function ( actual, message ) {
339 QUnit.push( actual === true, actual, true, message );
340 },
341
342 // Expect boolean false
343 assertFalse: function ( actual, message ) {
344 QUnit.push( actual === false, actual, false, message );
345 },
346
347 // Expect numerical value less than X
348 lt: function ( actual, expected, message ) {
349 QUnit.push( actual < expected, actual, 'less than ' + expected, message );
350 },
351
352 // Expect numerical value less than or equal to X
353 ltOrEq: function ( actual, expected, message ) {
354 QUnit.push( actual <= expected, actual, 'less than or equal to ' + expected, message );
355 },
356
357 // Expect numerical value greater than X
358 gt: function ( actual, expected, message ) {
359 QUnit.push( actual > expected, actual, 'greater than ' + expected, message );
360 },
361
362 // Expect numerical value greater than or equal to X
363 gtOrEq: function ( actual, expected, message ) {
364 QUnit.push( actual >= expected, actual, 'greater than or equal to ' + expected, message );
365 },
366
367 /**
368 * Asserts that two HTML strings are structurally equivalent.
369 *
370 * @param {string} actualHtml Actual HTML markup.
371 * @param {string} expectedHtml Expected HTML markup
372 * @param {string} message Assertion message.
373 */
374 htmlEqual: function ( actualHtml, expectedHtml, message ) {
375 var actual = getHtmlStructure( actualHtml ),
376 expected = getHtmlStructure( expectedHtml );
377
378 QUnit.push(
379 QUnit.equiv(
380 actual,
381 expected
382 ),
383 actual,
384 expected,
385 message
386 );
387 },
388
389 /**
390 * Asserts that two HTML strings are not structurally equivalent.
391 *
392 * @param {string} actualHtml Actual HTML markup.
393 * @param {string} expectedHtml Expected HTML markup.
394 * @param {string} message Assertion message.
395 */
396 notHtmlEqual: function ( actualHtml, expectedHtml, message ) {
397 var actual = getHtmlStructure( actualHtml ),
398 expected = getHtmlStructure( expectedHtml );
399
400 QUnit.push(
401 !QUnit.equiv(
402 actual,
403 expected
404 ),
405 actual,
406 expected,
407 message
408 );
409 }
410 };
411
412 $.extend( QUnit.assert, addons );
413
414 /**
415 * Small test suite to confirm proper functionality of the utilities and
416 * initializations defined above in this file.
417 */
418 QUnit.module( 'test.mediawiki.qunit.testrunner', QUnit.newMwEnvironment( {
419 setup: function () {
420 this.mwHtmlLive = mw.html;
421 mw.html = {
422 escape: function () {
423 return 'mocked';
424 }
425 };
426 },
427 teardown: function () {
428 mw.html = this.mwHtmlLive;
429 },
430 config: {
431 testVar: 'foo'
432 },
433 messages: {
434 testMsg: 'Foo.'
435 }
436 } ) );
437
438 QUnit.test( 'Setup', 3, function ( assert ) {
439 assert.equal( mw.html.escape( 'foo' ), 'mocked', 'setup() callback was ran.' );
440 assert.equal( mw.config.get( 'testVar' ), 'foo', 'config object applied' );
441 assert.equal( mw.messages.get( 'testMsg' ), 'Foo.', 'messages object applied' );
442
443 mw.config.set( 'testVar', 'bar' );
444 mw.messages.set( 'testMsg', 'Bar.' );
445 } );
446
447 QUnit.test( 'Teardown', 2, function ( assert ) {
448 assert.equal( mw.config.get( 'testVar' ), 'foo', 'config object restored and re-applied after test()' );
449 assert.equal( mw.messages.get( 'testMsg' ), 'Foo.', 'messages object restored and re-applied after test()' );
450 } );
451
452 QUnit.test( 'Loader status', 2, function ( assert ) {
453 var i, len, state,
454 modules = mw.loader.getModuleNames(),
455 error = [],
456 missing = [];
457
458 for ( i = 0, len = modules.length; i < len; i++ ) {
459 state = mw.loader.getState( modules[i] );
460 if ( state === 'error' ) {
461 error.push( modules[i] );
462 } else if ( state === 'missing' ) {
463 missing.push( modules[i] );
464 }
465 }
466
467 assert.deepEqual( error, [], 'Modules in error state' );
468 assert.deepEqual( missing, [], 'Modules in missing state' );
469 } );
470
471 QUnit.test( 'htmlEqual', 8, function ( assert ) {
472 assert.htmlEqual(
473 '<div><p class="some classes" data-length="10">Child paragraph with <a href="http://example.com">A link</a></p>Regular text<span>A span</span></div>',
474 '<div><p data-length=\'10\' class=\'some classes\'>Child paragraph with <a href=\'http://example.com\' >A link</a></p>Regular text<span>A span</span></div>',
475 'Attribute order, spacing and quotation marks (equal)'
476 );
477
478 assert.notHtmlEqual(
479 '<div><p class="some classes" data-length="10">Child paragraph with <a href="http://example.com">A link</a></p>Regular text<span>A span</span></div>',
480 '<div><p data-length=\'10\' class=\'some more classes\'>Child paragraph with <a href=\'http://example.com\' >A link</a></p>Regular text<span>A span</span></div>',
481 'Attribute order, spacing and quotation marks (not equal)'
482 );
483
484 assert.htmlEqual(
485 '<label for="firstname" accesskey="f" class="important">First</label><input id="firstname" /><label for="lastname" accesskey="l" class="minor">Last</label><input id="lastname" />',
486 '<label for="firstname" accesskey="f" class="important">First</label><input id="firstname" /><label for="lastname" accesskey="l" class="minor">Last</label><input id="lastname" />',
487 'Multiple root nodes (equal)'
488 );
489
490 assert.notHtmlEqual(
491 '<label for="firstname" accesskey="f" class="important">First</label><input id="firstname" /><label for="lastname" accesskey="l" class="minor">Last</label><input id="lastname" />',
492 '<label for="firstname" accesskey="f" class="important">First</label><input id="firstname" /><label for="lastname" accesskey="l" class="important" >Last</label><input id="lastname" />',
493 'Multiple root nodes (not equal, last label node is different)'
494 );
495
496 assert.htmlEqual(
497 'fo&quot;o<br/>b&gt;ar',
498 'fo"o<br/>b>ar',
499 'Extra escaping is equal'
500 );
501 assert.notHtmlEqual(
502 'foo&lt;br/&gt;bar',
503 'foo<br/>bar',
504 'Text escaping (not equal)'
505 );
506
507 assert.htmlEqual(
508 'foo<a href="http://example.com">example</a>bar',
509 'foo<a href="http://example.com">example</a>bar',
510 'Outer text nodes are compared (equal)'
511 );
512
513 assert.notHtmlEqual(
514 'foo<a href="http://example.com">example</a>bar',
515 'foo<a href="http://example.com">example</a>quux',
516 'Outer text nodes are compared (last text node different)'
517 );
518
519 } );
520
521 QUnit.module( 'test.mediawiki.qunit.testrunner-after', QUnit.newMwEnvironment() );
522
523 QUnit.test( 'Teardown', 3, function ( assert ) {
524 assert.equal( mw.html.escape( '<' ), '&lt;', 'teardown() callback was ran.' );
525 assert.equal( mw.config.get( 'testVar' ), null, 'config object restored to live in next module()' );
526 assert.equal( mw.messages.get( 'testMsg' ), null, 'messages object restored to live in next module()' );
527 } );
528
529 }( jQuery, mediaWiki, QUnit ) );